﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.OA.Biz;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Web.Controllers
{
    public class MemberController : Controller
    {
        //
        // GET: /Member/
        #region 会员相关
        
        
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetMemberByUserName(string userName)
        {
            var member = MemberBiz.GetMemberByUserName(userName);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (member == null)
            {
                dic.Add("RID", "0");
                dic.Add("Data","");
            }
            else
            {
                dic.Add("RID", "1");
                dic.Add("Data", member);
            }
            return Content(dic.ToJson());
        }

        [HttpPost]
        public ActionResult GetMemberListByPage(int page, int pageSize, string userName = "",
            string idCardNo = "", string email = "", string handPhone = "", int memberType = -1, int activeStatus=-1)
        {
            int count = 0;
            var userList = MemberBiz.GetMemberByPage(page, pageSize, userName, memberType,activeStatus, ref count);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", userList);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }


        [HttpPost]
        public ActionResult MemberChangeStatus(string userName, int status)
        {

            var user = MemberBiz.ChangeStatus(userName, status);

            if (user == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }
        #endregion
        #region 经纪人相关
        public ActionResult Agent()
        {
            return View();
        }
        [HttpPost]
        public ActionResult GetAgentByPage(int page, int pageSize, string userName = "",
            string idCardNo = "", string email = "", string handPhone = "")
        {
            int count = 0;
            var userList = MemberBiz.GetAgentByPage(page, pageSize, userName, ref count);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", userList);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

        [HttpPost]
        public ActionResult AgentSave(string userName, string realName, string idCardNo, string handPhone, string address, int optype, int settlementType, decimal settlement)
        {
            Gender gender=Gender.Unknow;

            if (SupportBiz.CheckIDCardNo(idCardNo, ref gender))
            {
                if (optype == 1)
                {
                    var user = MemberBiz.EditAgent(userName, realName, idCardNo, handPhone, address, gender, settlementType, settlement);

                    if (user == null)
                    {
                        return Content("0");
                    }
                    else
                    {
                        return Content("1");
                    }
                }
                else
                {
                    if (MemberBiz.AgentExsit(userName))
                    {
                        return Content("-1");
                    }

                    var user = MemberBiz.AddAgent(userName, realName, idCardNo, handPhone, address, System.Web.HttpContext.Current.User.Identity.Name, gender, settlementType, settlement);
                    if (user == null)
                    {
                        return Content("0");
                    }
                    else
                    {
                        return Content("1");
                    }
                }
            }
            else
            {
                return Content("-2");
            }
        }
        [HttpPost]
        public ActionResult AgentDelete(string userNames)
        {
            foreach (string userName in userNames.Split(','))
            {
                MemberBiz.DeleteAgent(userName);
            }

            return Content("1");
        }

        [HttpPost]
        public ActionResult AgentChangeStatus(string userName, int status)
        {

            var user = MemberBiz.ChangeAgentStatus(userName, status);

            if (user == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }
        [HttpPost]
        public ActionResult GetAgentByUserName(string userName)
        {
            var member = MemberBiz.GetAgentByUserName(userName);
            Dictionary<string, object> dic = new Dictionary<string, object>();
            if (member == null)
            {
                dic.Add("RID", "0");
                dic.Add("Data", "");
            }
            else
            {
                dic.Add("RID", "1");
                dic.Add("Data", member);
            }
            return Content(dic.ToJson());
        }

        #endregion
    }
}
